package com.xhtranslate.service.ali.controller;

import com.xhtranslate.common.dto.ImageFormDTO;
import com.xhtranslate.common.dto.TextFormDTO;
import com.xhtranslate.common.vo.ResponseEnum;
import com.xhtranslate.common.vo.ResponseVO;
import com.xhtranslate.service.ali.service.AliTranslateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author liuxuhuang
 * @version 1.0
 * @package com.xhtranslate.service.ali.controller
 * @date 2022/11/20 14:18:20
 * @description 阿里翻译控制器
 */
@RestController
@Api(value = "阿里翻译请求接口", tags = "阿里翻译请求接口")
@RequestMapping(value = "aliTranslate")
@Slf4j
public class AliTranslateController {
    @Autowired
    private AliTranslateService aliTranslateService;

    @PostMapping(value = "commonTranslate")
    @ApiOperation(value = "通用翻译")
    public ResponseVO commonTranslate(@RequestBody @Validated TextFormDTO textFormDTO) throws Exception {
        log.warn("阿里文本翻译：" + textFormDTO);
        String translate = aliTranslateService.commonTranslate(textFormDTO);
        if ("".equals(translate) || StringUtils.isBlank(translate)) {
            return ResponseVO.exception(ResponseEnum.RESULTNULL);
        }
        return ResponseVO.succ(translate);
    }

    @PostMapping(value = "imageTranslate")
    @ApiOperation(value = "图片翻译")
    public ResponseVO imageTranslate(@Validated ImageFormDTO dto) throws Exception {
        log.warn("阿里图片翻译：" + dto);
        String translate = aliTranslateService.imageTranslate(dto);
        if ("".equals(translate) || StringUtils.isBlank(translate)) {
            return ResponseVO.exception(ResponseEnum.OCRRESULTNULL);
        }
        if (translate.equals(ResponseEnum.OCRRESULTNULL.getMsg())) {
            return ResponseVO.exception(ResponseEnum.OCRRESULTNULL);
        }
        return ResponseVO.succ(translate);
    }
}
